/********************************************************************************
* File Name:	vipform.cpp
* Description:	第8章实例训练          
* Reference book:《Linux环境下Qt4图形界面与MySQL编程》，机械工业出版社.2012.1  
* E_mail: openlinux2011@gmail.com		                
*
********************************************************************************/
#include <QtGui>
#include <QSqlDatabase>
#include <QSqlQuery>
#include "vipform.h"
#include "ui_vipform.h"

//会员界面类构造函数
vipform::vipform(QWidget *parent) :QDialog(parent),ui(new Ui::vipform)
{
    ui->setupUi(this);
    QSqlQuery query  ;
    query.exec("select id from Customer");
    ui->cbvipid->clear();
    while(query.next())  //动态绑定会员号
    {
        ui->cbvipid->insertItem(0,query.value(0).toString());
    }
    queryvip();//显示当前选定会员id的会员信息
}

vipform::~vipform()
{
    delete ui;
}

void vipform::queryvip() //根据id查询会员信息
{
    QSqlQuery query  ;
    QString select="select * from Customer where id=";
    select +=ui->cbvipid->currentText();  //在查询语句中使用变量
    query.exec(select); //执行查询
    query.next();

    /*将会员的信息显示在对应的信息框中*/
    QString name = query.value(1).toString();
    ui->lename->setText(name );  //将查询到的名字送到会员名中
    int sex = query.value(2).toInt();//设置性别
    if (sex==1)
    {
        ui->rbboy->setChecked(TRUE);
    }
    else
    {
        ui->rbgirl->setChecked(TRUE);
    }
    QString birth=query.value(3).toString(); //显示生日
    ui->lebirth->setText(birth);
    QString degree = query.value(4).toString(); //显示学历
    int index=ui->cbdegree->findText(degree);
    ui->cbdegree->setCurrentIndex(index);  
    QString addr = query.value(5).toString(); //显示地址
    ui->leaddr->setText(addr);
    QString phone = query.value(6).toString(); //显示电话
    ui->lephone->setText(phone);   
    QString email = query.value(7).toString(); //显示Email
    ui->leemail->setText(email);    
    int grade = query.value(8).toInt(); //显示积分
    ui->sbgrade->setValue(grade);    
    int level= query.value(9).toInt(); //显示级别
    ui->sblevel->setValue(level);
}

void vipform::addvip() //增加新的会员
{ 
    QSqlQuery query;
    QString insertStr;
    int sex=0;
    if(ui->rbboy->isChecked()) //获取性别信息
        sex=1;
    else if(ui->rbgirl->isChecked())
        sex=0;
    insertStr="insert into Customer(Name,Sex,Birthday,Degree,Address,Telephone,Email,Score,Levels) values('"+ui->lename->text()+"',"+tr("%1").arg(sex)+",'"+ui->lebirth->text()+"','"+ui->cbdegree->currentText()+"','"+ui->leaddr->text()+"','"+ui->lephone->text()+"','"+ui->leemail->text()+"',"+tr("%1").arg(ui->sbgrade->value())+","+tr("%1").arg(ui->sblevel->value())+")";
    if(query.exec(insertStr)) //执行插入操作
    {
        QMessageBox mb( tr("Notice"),tr("添加成功"),QMessageBox::Question,QMessageBox::Ok ,QMessageBox::NoButton,QMessageBox::NoButton );
        mb.exec();
    }
    else
    {
       QMessageBox mb( "Notice",tr("添加失败"),QMessageBox::Question,QMessageBox::Ok ,QMessageBox::NoButton,QMessageBox::NoButton );
       mb.exec();
    }
    refreshvip(); //刷新会员信息
}

void vipform::save() //保存会员信息的更改
{
    QSqlQuery query;
    QString updateStr;
    int sex=0;
    if(ui->rbboy->isChecked()) //获取性别信息
        sex=1;
    else if(ui->rbgirl->isChecked())
        sex=0;
    QString id=ui->cbvipid->currentText();
    updateStr="update Customer set Name='"+ui->lename->text()+"',Sex="+tr("%1").arg(sex)+",Birthday='"+ui->lebirth->text()+"',Degree='"+ui->cbdegree->currentText()+"',Address='"+ui->leaddr->text()+"',Telephone='"+ui->lephone->text()+"',Email='"+ui->leemail->text()+"',Score="+tr("%1").arg(ui->sbgrade->value())+",Levels="+tr("%1").arg(ui->sblevel->value())+" where id="+id;   
    if(query.exec(updateStr)) //执行更改
    {
        QMessageBox mb( "Notice",tr("保存成功"),QMessageBox::Question,QMessageBox::Ok ,QMessageBox::NoButton,QMessageBox::NoButton );
        mb.exec();
    }
    else
    {
       QMessageBox mb( "Notice",tr("保存失败"),QMessageBox::Question,QMessageBox::Ok ,QMessageBox::NoButton,QMessageBox::NoButton );
       mb.exec();
    }
}


void vipform::refreshvip() //将最新的会员ID绑定到QComboBox中
{
    QSqlQuery query  ;
    query.exec("select id from Customer"); //选择ID
    ui->cbvipid->clear();
    while(query.next())  //动态绑定会员号
    {
        ui->cbvipid->insertItem(0,query.value(0).toString());
    }
    queryvip(); //查询会员信息
}


void vipform::deleteVip() //删除会员
{

    QSqlQuery query  ;
    if(query.exec("delete from Customer where id="+ui->cbvipid->currentText())) //执行删除操作
    {
        QMessageBox mb( "Notice",tr("删除成功"),QMessageBox::Question,QMessageBox::Ok ,QMessageBox::NoButton,QMessageBox::NoButton );
        mb.exec();
    }
    else
    {
       QMessageBox mb( "Notice",tr("删除失败"),QMessageBox::Question,QMessageBox::Ok ,QMessageBox::NoButton,QMessageBox::NoButton );
       mb.exec();
    }
    refreshvip(); //刷新
}

void vipform::clear() //清空显示框信息
{
    ui->lename->setText("");
    ui->rbboy->setChecked(TRUE);
    ui->rbgirl->setChecked(FALSE);
    ui->lebirth->setText("");
    ui->cbdegree->setCurrentIndex(0);
    ui->leaddr->setText("");
    ui->lephone->setText("");
    ui->leemail->setText("");
    ui->sbgrade->setValue(0);
    ui->sblevel->setValue(0);
}
